﻿#include "puywindow.h"
#include "ui_puywindow.h"

PuyWindow::PuyWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::PuyWindow)
{
    ui->setupUi(this);

    //读取用户文件，将单个商品信息存储到goodStringList， 全部商品信息存储到goodList
    QFile file("./good.txt");

    if(!file.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    while(!file.atEnd())
    {
        QString str = file.readLine();
        goodStringList = str.split("-");
        goodList << goodStringList;
    }
    file.close();

    //左侧菜单栏按钮样式
    ui->buyGoodInterfBtn->setStyleSheet("QPushButton#buyGoodInterfBtn{background-color: rgb(58, 58, 58);"
                                      "color: rgb(255, 255, 255);border-top-width:10px;"
                                      "border-top-color: rgb(125, 125, 125);"
                                      "border-style:solid;}"
                                      "QPushButton#buyGoodInterfBtn:hover{background-color: rgb(140, 140, 140);"
                                      "color: rgb(255, 255, 255);border-top-width:10px;"
                                      "border-top-color: rgb(125, 125, 125);"
                                      "border-style:solid;}"
                                      "QPushButton#buyGoodInterfBtn:pressed{background-color: rgb(90, 90, 90);"
                                      "color: rgb(255, 255, 255);border-top-width:10px;"
                                      "border-top-color: rgb(125, 125, 125);"
                                      "border-style:solid;}");

    ui->shoppingCartInterfaBtn->setStyleSheet("QPushButton#shoppingCartInterfaBtn{background-color: rgb(58, 58, 58);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}"
                                     "QPushButton#shoppingCartInterfaBtn:hover{background-color: rgb(140, 140, 140);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}"
                                     "QPushButton#shoppingCartInterfaBtn:pressed{background-color: rgb(90, 90, 90);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}");

    ui->buyRecordInterfaBtn->setStyleSheet("QPushButton#buyRecordInterfaBtn{background-color: rgb(58, 58, 58);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}"
                                     "QPushButton#buyRecordInterfaBtn:hover{background-color: rgb(140, 140, 140);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}"
                                     "QPushButton#buyRecordInterfaBtn:pressed{background-color: rgb(90, 90, 90);"
                                     "color: rgb(255, 255, 255);border-top-width:10px;"
                                     "border-top-color: rgb(125, 125, 125);"
                                     "border-style:solid;}");

    ui->contactBtn->setStyleSheet("QPushButton#contactBtn{background-color: rgb(58, 58, 58);"
                                 "color: rgb(255, 255, 255);border-top-width:10px;"
                                 "border-top-color: rgb(125, 125, 125);"
                                 "border-style:solid;}"
                                 "QPushButton#contactBtn:hover{background-color: rgb(140, 140, 140);"
                                 "color: rgb(255, 255, 255);border-top-width:10px;"
                                 "border-top-color: rgb(125, 125, 125);"
                                 "border-style:solid;}"
                                 "QPushButton#contactBtn:pressed{background-color: rgb(90, 90, 90);"
                                 "color: rgb(255, 255, 255);border-top-width:10px;"
                                 "border-top-color: rgb(125, 125, 125);"
                                 "border-style:solid;}");

/*
 * 商品购买界面优化
*/

    //搜索按钮初始化
//    ui->searchBtn->setStyleSheet("QPushButton#searchBtn{border-image: url(:/pic/search.png);}"
//                                 "QPushButton#searchBtn:hover{border-image: url(:/pic/search2.png);}"
//                                 "QPushButton#searchBtn:pressed{border-image: url(:/pic/search3.png);}");

    //清空搜索按钮初始化
//    ui->cancelBtn->setStyleSheet("QPushButton#cancelBtn{border-image: url(:/pic/cancel.png);}"
//                                 "QPushButton#cancelBtn:hover{border-image: url(:/pic/cancel2.png);}"
//                                 "QPushButton#cancelBtn:pressed{border-image: url(:/pic/cancel3.png);}");

    //刷新按钮初始化
    ui->reflushBtn->setStyleSheet("QPushButton#reflushBtn{border-image: url(:/pic/刷新.png);}"
                                 "QPushButton#reflushBtn:hover{border-image: url(:/pic/刷新2.png);}"
                                 "QPushButton#reflushBtn:pressed{border-image: url(:/pic/刷新3.png);}");

/*
 * 商品购买界面优化
*/
    //刷新按钮初始化
    ui->reflushCartBtn->setStyleSheet("QPushButton#reflushCartBtn{border-image: url(:/pic/刷新.png);}"
                                 "QPushButton#reflushCartBtn:hover{border-image: url(:/pic/刷新2.png);}"
                                 "QPushButton#reflushCartBtn:pressed{border-image: url(:/pic/刷新3.png);}");


    //显示欢迎页面
    ui->widget_3->show();
    totelPrices = 0;
    totelPricesCart = 0;
    ui->moneyLabel->setText(QString::number(totelPrices));
    ui->textBrowser->hide();



/*
 * 表组件初始化
*/

    //商品购买界面组件初始化
    ui->goodsTableWidget->setColumnWidth(0, 138);
    ui->goodsTableWidget->setColumnWidth(1, 138);
    ui->goodsTableWidget->setColumnWidth(2, 138);
    ui->goodsTableWidget->setColumnWidth(3, 122);
    ui->goodsTableWidget->setColumnWidth(4, 138);

    //购物车界面组件初始化
    ui->cartTableWidget->setColumnWidth(0, 172);
    ui->cartTableWidget->setColumnWidth(1, 172);
    ui->cartTableWidget->setColumnWidth(2, 172);
    ui->cartTableWidget->setColumnWidth(3, 155);

    //订单界面组件初始化
    ui->orderTableWidget->setColumnWidth(0, 100);
    ui->orderTableWidget->setColumnWidth(1, 100);
    ui->orderTableWidget->setColumnWidth(2, 100);
    ui->orderTableWidget->setColumnWidth(3, 80);
    ui->orderTableWidget->setColumnWidth(4, 103);
    ui->orderTableWidget->setColumnWidth(5, 95);
    ui->orderTableWidget->setColumnWidth(6, 95);


    //隐藏组件

    //商品购买界面
//    ui->goodComboBox->hide();
//    ui->searchEdit->hide();
//    ui->searchBtn->hide();
    ui->goodsTableWidget->hide();
//    ui->cancelBtn->hide();
    ui->reflushBtn->hide();
    ui->label_6->hide();
    ui->moneyLabel->hide();
    ui->reflushCartBtn->hide();

    //购物车界面
    ui->label_7->hide();
    ui->cartTableWidget->hide();
    ui->cartBuyBtn->hide();
    ui->cartMoneyLabel->hide();
    ui->widget_4->hide();

    //订单界面
    ui->orderTableWidget->hide();
    ui->widget_5->hide();
}

PuyWindow::~PuyWindow()
{
    delete ui;
}

/*
 *  获取登录信息
*/

void PuyWindow::getUserInfo(const QString &userName, const int &userIndex)
{
    this->u_Name = userName;
    this->u_Index = userIndex;
    ui->nameLabel->setText(u_Name);
}


/*
 *  关闭时，弹出询问提示框，确定关闭后修改用户登录状态
*/
void PuyWindow::closeEvent(QCloseEvent *event)
{
    //读取用户文件
    QList<QStringList> c_userList;
    QFile file("./user.txt");

    if(!file.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    while(!file.atEnd())
    {
        QString str = file.readLine();
        QStringList c_userStringList = str.split("-");
        c_userList << c_userStringList;
    }
    file.close();
    //窗口关闭时询问是否退出
   QMessageBox::StandardButton result=QMessageBox::question(this, "退出", "确定要退出本系统吗？",
                      QMessageBox::Yes|QMessageBox::No |QMessageBox::Cancel,
                      QMessageBox::No);

    if (result==QMessageBox::Yes)
    {
        //修改用户登录状态为1，表示用户已经登录不得重复登录

        for (int i = 0 ; i < c_userList.size(); i++)
        {
            if(c_userList[i].at(2).toInt() == u_Index)
            {
                if(c_userList[i].at(0) == u_Name)
                {
                    QStringList info;
                    info << c_userList[i].at(0) << c_userList[i].at(1) << c_userList[i].at(2) << "0\r\n";
                    c_userList.replace(i, info);
                    QFile file_close("./user.txt");

                    if(!file_close.open(QIODevice::ReadWrite))
                    {
                        qDebug() << "打开文件失败";
                    }
                    file_close.resize(0);
                    for(int i = 0; i < c_userList.size(); i++)
                    {
                        QString str = c_userList[i].at(0) + "-" + c_userList[i].at(1) + "-" + c_userList[i].at(2) + "-" + c_userList[i].at(3);
                        qDebug() << str;
                        file_close.write(str.toUtf8());
                    }

                    file_close.close();
                }
            }
        }
        event->accept();
    }

    else
        event->ignore();
}

/*
 *商品购买界面模块：
 *          功能：商品信息查看、商品选购
 *          函数：
 *              void PuyWindow::getAdd(const int &row, const int &count) 选购商品数量增加1
 *              void PuyWindow::getSub(const int &row, const int &count) 选购商品数量减少1
 *              void PuyWindow::updataGood() （*该函数未使用)
 *              void PuyWindow::on_buyGoodInterfBtn_clicked()点击商品购买界面
 *              void PuyWindow::on_reflushBtn_clicked() 刷新商品信息
 *
*/

void PuyWindow::getAdd(const int &row, const int &count)
{
    qDebug() << "第" << row << "行";
    qDebug() << "购买了" << count;

    //计算总价
    float p_prices = goodList[row].at(1).toFloat();
    totelPrices += p_prices;
    ui->moneyLabel->setText(QString::number(totelPrices));

    //将购买的商品行号及对应的数量分别放到对应的列表中
    QStringList tmpStringList;

    cartList = cartMap.value(u_Name);
    qDebug() << "cartList size:" << cartList.size();
    if(cartList.size() == 0)
    {
        QString name = goodList[row].at(0);
        QString prices = goodList[row].at(1);
        QString kind = goodList[row].at(2);
        int num = count;
        tmpStringList << name << prices << kind << QString::number(num);
        cartList.push_back(tmpStringList);
    }
    else
    {
        int flag_1 = -1;
        for(int i = 0; i < cartList.size(); i++)
        {
            qDebug() << "cartList i.at(0):" << cartList[i].at(0);
            qDebug() << "goodList[row].at(0):" << goodList[row].at(0);
            if(cartList[i].at(0) == goodList[row].at(0))
            {
                QString tmpName = cartList[i].at(0);
                QString tmpPrices = cartList[i].at(1);
                QString tmpKind = cartList[i].at(2);
                int tmpNum =cartList[i].at(3).toInt() + 1;
                tmpStringList << tmpName << tmpPrices << tmpKind << QString::number(tmpNum);
                cartList.replace(i, tmpStringList);
                flag_1 = 1;
            }
        }
        if(flag_1 == -1)
        {
            QString name = goodList[row].at(0);
            QString prices = goodList[row].at(1);
            QString kind = goodList[row].at(2);
            int num = count;
            tmpStringList << name << prices << kind << QString::number(num);
            cartList.push_back(tmpStringList);
        }
    }
    int flag = 0;
    QMap<QString, QList<QStringList>>::const_iterator i;
    for(i=cartMap.constBegin(); i!=cartMap.constEnd(); ++i)
    {
        if(i.key() == u_Name)
        {
            cartMap[u_Name] = cartList;
            flag = 1;
        }
    }

    if(flag == 0)
        cartMap.insert(u_Name, cartList);

    qDebug() << "cartBuyMap:" << cartMap;

    updataMap();
    //将库存数量写入文件
    int inventory = goodList[row].at(3).toInt();
    ui->goodsTableWidget->item(row, 3)->setText(QString::number(--inventory));
    QString num = QString::number(inventory) + "\r\n";
    QStringList tmp;
    tmp << goodList[row].at(0) << goodList[row].at(1) << goodList[row].at(2) << num;
    goodList.replace(row, tmp);
    QFile file_add("./good.txt");

    if(!file_add.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }

    file_add.resize(0);
    for(int i = 0; i < goodList.size(); i++)
    {
        QString str = goodList[i].at(0) + "-" + goodList[i].at(1) + "-" + goodList[i].at(2) + "-" + goodList[i].at(3);
        file_add.write(str.toUtf8());
    }
    file_add.close();

}

void PuyWindow::getSub(const int &row, const int &count)
{
    qDebug() << "第" << row << "行";
    qDebug() << "购买了" << count;


    //计算总价
    float p_prices = goodList[row].at(1).toFloat();
    totelPrices -= p_prices;
    ui->moneyLabel->setText(QString::number(totelPrices));

    //将购买的商品行号及对应的数量分别放到对应的列表中
    QStringList tmpStringList;

    cartList = cartMap.value(u_Name);
    if(cartList.size() == 0)
    {
        QString name = goodList[row].at(0);
        QString prices = goodList[row].at(1);
        QString kind = goodList[row].at(2);
        int num = count;
        tmpStringList << name << prices << kind << QString::number(num);
        cartList.push_back(tmpStringList);
    }
    else
    {
        int flag_1 = -1;
        for(int i = 0; i < cartList.size(); i++)
        {
            qDebug() << "cartList i.at(0):" << cartList[i].at(0);
            qDebug() << "goodList[row].at(0):" << goodList[row].at(0);
            if(cartList[i].at(0) == goodList[row].at(0))
            {
                QString tmpName = cartList[i].at(0);
                QString tmpPrices = cartList[i].at(1);
                QString tmpKind = cartList[i].at(2);
                int tmpNum = cartList[i].at(3).toInt() - 1;
                tmpStringList << tmpName << tmpPrices << tmpKind << QString::number(tmpNum);
                cartList.replace(i, tmpStringList);
                flag_1 = 1;
            }
        }
        if(flag_1 == -1)
        {
            QString name = goodList[row].at(0);
            QString prices = goodList[row].at(1);
            QString kind = goodList[row].at(2);
            int num = count;
            tmpStringList << name << prices << kind << QString::number(num);
            cartList.push_back(tmpStringList);
        }
    }
    int flag = 0;
    QMap<QString, QList<QStringList>>::const_iterator i;
    for(i=cartMap.constBegin(); i!=cartMap.constEnd(); ++i)
    {
        if(i.key() == u_Name)
        {
            cartMap[u_Name] = cartList;
            flag = 1;
        }
    }

    if(flag == 0)
        cartMap.insert(u_Name, cartList);

    qDebug() << "cartBuyMap:" << cartMap;

    updataMap();
    //将库存数量写入文件
    int inventory = goodList[row].at(3).toInt();
    ui->goodsTableWidget->item(row, 3)->setText(QString::number(++inventory));
    QString num = QString::number(inventory) + "\r\n";
    QStringList tmp;
    tmp << goodList[row].at(0) << goodList[row].at(1) << goodList[row].at(2) << num;
    goodList.replace(row, tmp);
    QFile file_add("./good.txt");

    if(!file_add.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }

    file_add.resize(0);
    for(int i = 0; i < goodList.size(); i++)
    {
        QString str = goodList[i].at(0) + "-" + goodList[i].at(1) + "-" + goodList[i].at(2) + "-" + goodList[i].at(3);
        file_add.write(str.toUtf8());
    }
    file_add.close();



}

void PuyWindow::updataGood()
{
    int index = -1;
    int count = 0;
    QMap<QString, QList<QStringList>>::const_iterator i;
    for(i=cartMap.constBegin(); i!=cartMap.constEnd(); ++i)
    {
        if(index != -1)
            break;
        if(i.key() == u_Name)
        {
            QList<QStringList> tmpList = i.value();
            for ( int i = 0; i < tmpList.size(); i++)
            {
                if(index != -1)
                    break;
                for(int j = 0; j < goodList.size(); j++)
                {
                    if(goodList[j].at(0) == tmpList[i].at(0))
                    {
                        index = j;
                        count = tmpList[i].at(3).toInt();
                        break;
                    }
                }
            }

        }
    }

    int inventory = goodList[index].at(3).toInt();
    ui->goodsTableWidget->item(index, 3)->setText(QString::number(inventory - count));
    QString num = QString::number(inventory) + "\r\n";
    QStringList tmp;
    tmp << goodList[index].at(0) << goodList[index].at(1) << goodList[index].at(2) << num;
    goodList.replace(index, tmp);
    QFile file_add("./good.txt");

    if(!file_add.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }

    file_add.resize(0);
    for(int i = 0; i < goodList.size(); i++)
    {
        QString str = goodList[i].at(0) + "-" + goodList[i].at(1) + "-" + goodList[i].at(2) + "-" + goodList[i].at(3);
        file_add.write(str.toUtf8());
    }
    file_add.close();

}

void PuyWindow::on_buyGoodInterfBtn_clicked()
{
    totelPrices = 0;
    totelPricesCart = 0;
    ui->moneyLabel->setText(QString::number(totelPrices));
    //显示的组件
//    ui->goodComboBox->show();
//    ui->searchEdit->show();
//    ui->searchBtn->show();
    ui->goodsTableWidget->show();
//    ui->cancelBtn->show();
    ui->reflushBtn->show();


    ui->label_6->show();
    ui->moneyLabel->show();
    //隐藏的组件
    ui->textBrowser->hide();
    ui->orderTableWidget->hide();
    ui->cartBuyBtn->hide();
    ui->widget_3->hide();
//    ui->searchEdit->clear();
    ui->widget_4->hide();
    ui->cartTableWidget->hide();
    ui->label_7->hide();
    ui->cartMoneyLabel->hide();
    ui->reflushCartBtn->hide();
    ui->widget_5->hide();
    goodList.clear();

    //初始化购物车存储列表
//    QFile file_cart("./shoppingcart.json");
//    if(!file_cart.open(QIODevice::ReadWrite))
//    {
//        qDebug() << "打开文件失败";
//    }
//    QByteArray arry = file_cart.readAll();
//    QJsonDocument doc = QJsonDocument::fromJson(arry);
//    QJsonObject obj = doc.object();
//    QJsonArray obj_arry = obj.value("users").toArray();
//    for (int i = 0; i < obj_arry.size(); i++)
//    {
//        QJsonObject userObj = obj_arry[i].toObject();
//        if(userObj.value("name").toString() == u_Name)
//        {
//            QJsonArray userArr = userObj.value("cart").toArray();
//            if(userArr.size() == 0)
//            {
//                tmpBuyList.clear();
//            }
//            else
//            {
//                for (int i = 0; i < userArr.size(); i++)
//                {
//                    QString str =  userArr[i].toString();
//                    QStringList tmpStringList = str.split("-");
//                    tmpBuyList.push_back(tmpStringList);
//                }
//            }
//        }
//    }



    //读取用户文件，将单个商品信息存储到goodStringList， 全部商品信息存储到goodList
    QFile file("./good.txt");

    if(!file.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    while(!file.atEnd())
    {
        QString str = file.readLine();
        goodStringList = str.split("-");
        goodList << goodStringList;
    }
    file.close();
    qDebug() << "读取文件完毕";
    qDebug() << "ListSize:" << goodList.size();
    qDebug() << "List:" << goodList;
    //设置表格的行数等于商品信息的数量
    ui->goodsTableWidget->setRowCount(goodList.size());

    //初始化表格中的内容
    for(int i = 0; i < goodList.size(); i++)
    {

        QTableWidgetItem* item1 = new QTableWidgetItem(goodList[i].at(0));
        QTableWidgetItem* item2 = new QTableWidgetItem(goodList[i].at(1));
        QTableWidgetItem* item3 = new QTableWidgetItem(goodList[i].at(2));
        QTableWidgetItem* item4 = new QTableWidgetItem(goodList[i].at(3));
        item1->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        item2->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        item3->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        item4->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        ui->goodsTableWidget->setItem(i,0,item1);
        ui->goodsTableWidget->setItem(i,1,item2);
        ui->goodsTableWidget->setItem(i,2,item3);
        ui->goodsTableWidget->setItem(i,3,item4);
        buyBtnForm* btn = new buyBtnForm;
        ui->goodsTableWidget->setRowHeight(i, 35);
        btn->resize(138, 35);
        ui->goodsTableWidget->setCellWidget(i, 4, btn);
        //将要修改或删除的行号通过信号和槽发送到控件中
        connect(this, &PuyWindow::sendRowBuyAdd, btn, &buyBtnForm::getRowBuyAdd);
        connect(this, &PuyWindow::sendRowBuySub, btn, &buyBtnForm::getRowBuySub);
        emit sendRowBuyAdd(0, i, goodList[i].at(3).toInt());
        emit sendRowBuySub(0, i, goodList[i].at(3).toInt());
        disconnect(this, &PuyWindow::sendRowBuyAdd, btn, &buyBtnForm::getRowBuyAdd);
        disconnect(this, &PuyWindow::sendRowBuySub, btn, &buyBtnForm::getRowBuySub);
        connect(btn, &buyBtnForm::sendRowAdd, this, &PuyWindow::getAdd);
        connect(btn, &buyBtnForm::sendRowSub, this, &PuyWindow::getSub);



    }

    ui->goodsTableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);

}

void PuyWindow::on_reflushBtn_clicked()
{
    on_buyGoodInterfBtn_clicked();
    totelPrices = 0;
}




/*
 * 购物车界面模块：
 *          功能：查看已选购商品、对已选购商品数量更改、购买生成订单
 *          函数：
 *              void PuyWindow::getAddCart(const int &row, const int &count) 商品数量加1
 *              void PuyWindow::getSubCart(const int &row, const int &count) 商品数量减1
 *              void PuyWindow::updataMap() 更新购物车信息
 *              void PuyWindow::on_shoppingCartInterfaBtn_clicked() 点击购物车界面
 *              void PuyWindow::on_reflushCartBtn_clicked() 刷新购物车按钮
 *              void PuyWindow::on_cartBuyBtn_clicked() 购买生成订单按钮
 *
*/

void PuyWindow::getAddCart(const int &row, const int &count)
{
    qDebug() << "购物车要加的行号：" << row;
    qDebug() << "该商品总数：" << count;
    float  t_prices = cartList[row].at(1).toFloat();
    totelPricesCart += t_prices;
    ui->cartMoneyLabel->setText(QString::number(totelPricesCart));
    //将购物车链表的数据更新
//    buyCountRecord.replace(row, buyCountRecord[row] + 1);
//    qDebug() << "buycount[row]" << buyCountRecord[row];
    QString name = cartList[row].at(0);
    QString prices = cartList[row].at(1);
    QString kind = cartList[row].at(2);
    QStringList tmpStringList;
    tmpStringList << name << prices << kind << QString::number(count);
    cartList.replace(row, tmpStringList);
    cartMap[u_Name] = cartList;
    qDebug() << "after cartMap:" << cartMap;
    updataMap();

    //将库存数量写入文件
    int index = -1;
    for (int i = 0; i < goodList.size(); i++)
    {
        if(goodList[i].at(0) == cartList[row].at(0))
        {
            index = i;
            break;
        }
    }
    if(index == -1)
        return;
    else
    {
        int inventory = goodList[index].at(3).toInt();
        QString num = QString::number(--inventory) + "\r\n";
        QStringList tmp;
        tmp << goodList[index].at(0) << goodList[index].at(1) << goodList[index].at(2) << num;
        qDebug() << "tmp:" << tmp;
        goodList.replace(index, tmp);
        QFile file_sub("./good.txt");

        if(!file_sub.open(QIODevice::ReadWrite))
        {
            qDebug() << "打开文件失败";
        }

        file_sub.resize(0);
        for(int i = 0; i < goodList.size(); i++)
        {
            QString str = goodList[i].at(0) + "-" + goodList[i].at(1) + "-" + goodList[i].at(2) + "-" + goodList[i].at(3);
            qDebug() << "str:" << str;
            file_sub.write(str.toUtf8());
        }
        file_sub.close();

    }


//    on_shoppingCartInterfaBtn_clicked();
}

void PuyWindow::getSubCart(const int &row, const int &count)
{

    qDebug() << "购物车要减的行号：" << row;
    qDebug() << "该商品总数：" << count;
    float  t_prices = cartList[row].at(1).toFloat();
    totelPricesCart -= t_prices;
    ui->cartMoneyLabel->setText(QString::number(totelPricesCart));
    //将购物车链表的数据更新
//    buyCountRecord.replace(row, buyCountRecord[row] - 1);
//    qDebug() << "buycount[row]" << buyCountRecord[row];

    QString name = cartList[row].at(0);
    QString prices = cartList[row].at(1);
    QString kind = cartList[row].at(2);
    QStringList tmpStringList;
    tmpStringList << name << prices << kind << QString::number(count);
    cartList.replace(row, tmpStringList);
    cartMap[u_Name] = cartList;
    qDebug() << "after cartMap:" << cartMap;
    updataMap();
    //将库存数量写入文件
    int index = -1;
    for (int i = 0; i < goodList.size(); i++)
    {
        if(goodList[i].at(0) == cartList[row].at(0))
        {
            index = i;
            break;
        }
    }
    if(index == -1)
        return;
    else
    {
        int inventory = goodList[index].at(3).toInt();
        QString num = QString::number(++inventory) + "\r\n";
        QStringList tmp;
        tmp << goodList[index].at(0) << goodList[index].at(1) << goodList[index].at(2) << num;
        qDebug() << "tmp:" << tmp;
        goodList.replace(index, tmp);
        QFile file_sub("./good.txt");

        if(!file_sub.open(QIODevice::ReadWrite))
        {
            qDebug() << "打开文件失败";
        }

        file_sub.resize(0);
        for(int i = 0; i < goodList.size(); i++)
        {
            QString str = goodList[i].at(0) + "-" + goodList[i].at(1) + "-" + goodList[i].at(2) + "-" + goodList[i].at(3);
            qDebug() << "str:" << str;
            file_sub.write(str.toUtf8());
        }
        file_sub.close();

    }




    //    on_shoppingCartInterfaBtn_clicked();
}

void PuyWindow::updataMap()
{

    QJsonObject cartObj;
    QJsonArray t_cartArry;
    QJsonObject obj;
    QMap<QString, QList<QStringList>>::const_iterator i;
    for( i=cartMap.constBegin(); i!=cartMap.constEnd(); ++i)
    {
        QJsonArray cartArry;

        QList<QStringList> tmpList;
        tmpList = i.value();
        qDebug() << "tmpList:" << tmpList;
//        if(tmpList.size() == 0)
//        {
//            continue;
//        }
//        else
//        {
            for (int i = 0; i <tmpList.size(); i++)
            {
                QStringList tmpStringList = tmpList[i];
                if(tmpStringList[3] == "0")
                    continue;
                else
                {
                    qDebug() << "tmpStringList:" << tmpStringList;
                    QString str = tmpStringList[0] + "-" + tmpStringList[1] + "-" + tmpStringList[2] + "-" + tmpStringList[3];
                    cartArry.push_back(str);
                }

            }
            cartObj.insert("name", i.key());
            cartObj.insert("cart", cartArry);

            t_cartArry.push_back(cartObj);
        }

//    }
    obj.insert("users", t_cartArry);
    qDebug() << "obj:" << obj;
    QJsonDocument doc(obj);
    QFile file_updata("./shoppingcart.json");
    if(!file_updata.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    file_updata.resize(0);
    file_updata.write(doc.toJson());
    file_updata.close();


}

void PuyWindow::on_shoppingCartInterfaBtn_clicked()
{
    totelPrices = 0;
    totelPricesCart = 0;
    //隐藏组件
//    ui->goodComboBox->hide();
//    ui->searchEdit->hide();
//    ui->searchBtn->hide();
    ui->goodsTableWidget->hide();
//    ui->cancelBtn->hide();
    ui->reflushBtn->hide();
    ui->orderTableWidget->hide();
    ui->widget_5->hide();
    ui->label_6->hide();
    ui->moneyLabel->hide();
    ui->textBrowser->hide();
    //显示组件
    ui->label_7->show();
    ui->cartTableWidget->show();
    ui->cartBuyBtn->show();
    ui->cartMoneyLabel->show();
    ui->reflushCartBtn->show();


    qDebug() << "buyrowrecord:" << buyRowRecord;
    qDebug() << "size:" << buyRowRecord.size();


    cartStringList.clear();
    cartList.clear();
    cartMap.clear();
    QFile file_cart("./shoppingcart.json");
    if(!file_cart.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    QByteArray arry = file_cart.readAll();
    QJsonDocument doc = QJsonDocument::fromJson(arry);
    QJsonObject obj = doc.object();
    QJsonArray obj_arry = obj.value("users").toArray();
    for (int i = 0; i < obj_arry.size(); i++)
    {
        QJsonObject userObj = obj_arry[i].toObject();
        if(userObj.value("name").toString() == u_Name)
        {
            QJsonArray userArr = userObj.value("cart").toArray();
            if(userArr.size() == 0)
            {
                ui->widget_4->show();
                ui->cartBuyBtn->hide();
                ui->cartTableWidget->setRowCount(userArr.size());
                QList<QStringList> t_cartList;
                cartMap.insert(u_Name, t_cartList);
            }
            else
            {
                //设置表格的行数等于用户信息的数量
                ui->cartTableWidget->setRowCount(userArr.size());
                int index = 0;
                for (int i = 0; i < userArr.size(); i++)
                {
                    QString str = userArr[i].toString();
                    cartStringList = str.split("-");
                    if(cartStringList[3] == "0")
                    {

                        ui->cartTableWidget->setRowCount(userArr.size() -1 );

                        continue;
                    }
                    else
                    {
                        cartList << cartStringList;
                        qDebug() << "cartStringList" << cartStringList;
                        qDebug() << "cartList" << cartList;
                        qDebug() << "row:" << cartStringList[0] << " "<< cartStringList[1] << " " << cartStringList[2] << " " << cartStringList[3];
                        qDebug() << "cartStringList.size:" << cartStringList.size();
                        QTableWidgetItem* item1 = new QTableWidgetItem(cartStringList[0]);
                        QTableWidgetItem* item2 = new QTableWidgetItem(cartStringList[1]);
                        QTableWidgetItem* item3 = new QTableWidgetItem(cartStringList[2]);
                        qDebug() << "1";
                        item1->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                        item2->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                        item3->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                        qDebug() << "2";
                        ui->cartTableWidget->setItem(index,0,item1);
                        ui->cartTableWidget->setItem(index,1,item2);
                        ui->cartTableWidget->setItem(index,2,item3);
                        qDebug() << "3";
                        buyBtnForm* btn = new buyBtnForm;
                        ui->cartTableWidget->setRowHeight(index, 35);
                        btn->resize(172, 35);
                        ui->cartTableWidget->setCellWidget(index, 3, btn);
                        qDebug() << "4";
                        //将要修改或删除的行号通过信号和槽发送到控件中
                        connect(this, &PuyWindow::sendRowBuyAddCart, btn, &buyBtnForm::getRowCartBuyAdd);
                        connect(this, &PuyWindow::sendRowBuySubCart, btn, &buyBtnForm::getRowCartBuySub);
                        emit sendRowBuyAddCart(1, index, cartStringList[3].toInt());
                        emit sendRowBuySubCart(1, index, cartStringList[3].toInt());
                        disconnect(this, &PuyWindow::sendRowBuyAddCart, btn, &buyBtnForm::getRowCartBuyAdd);
                        disconnect(this, &PuyWindow::sendRowBuySubCart, btn, &buyBtnForm::getRowCartBuySub);
                        connect(btn, &buyBtnForm::sendRowSubCart, this, &PuyWindow::getSubCart);
                        connect(btn, &buyBtnForm::sendRowAddCart, this, &PuyWindow::getAddCart);
                        qDebug() << "5";
                        totelPricesCart += (cartStringList[1].toFloat() * cartStringList[3].toUInt());
                        ui->cartMoneyLabel->setText(QString::number(totelPricesCart));
                        index++;
                    }

                }

                cartMap.insert(u_Name, cartList);

            }
        }
        else
        {
            QList<QStringList> t_cartList;
            QStringList t_cartStringList;
            QJsonArray userArr = userObj.value("cart").toArray();
            for (int i = 0; i <userArr.size(); i++)
            {
                QString str = userArr[i].toString();
                t_cartStringList = str.split("-");
                t_cartList << t_cartStringList;
            }
            cartMap.insert(userObj.value("name").toString(), t_cartList);
        }

    }
    qDebug() << "map:" << cartMap;


    //设置表格的行数等于用户信息的数量
//    ui->cartTableWidget->setRowCount(buyRowRecord.size());
//    if(buyRowRecord.size() == 0)
//        ui->widget_4->show();
//    else
//    {

//        for (int i = 0; i < buyRowRecord.size(); i++)
//        {

//            qDebug() << "row:" << goodList[buyRowRecord[i]].at(0) << " "<< goodList[buyRowRecord[i]].at(1) << " " << goodList[buyRowRecord[i]].at(2);
//            qDebug() << "cout.size:" << buyCountRecord.size();
//            qDebug() << "cout:" << buyCountRecord[i];
//            QTableWidgetItem* item1 = new QTableWidgetItem(goodList[buyRowRecord[i]].at(0));
//            QTableWidgetItem* item2 = new QTableWidgetItem(goodList[buyRowRecord[i]].at(1));
//            QTableWidgetItem* item3 = new QTableWidgetItem(goodList[buyRowRecord[i]].at(2));
//            qDebug() << "1";
//            item1->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
//            item2->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
//            item3->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
//            qDebug() << "2";
//            ui->cartTableWidget->setItem(i,0,item1);
//            ui->cartTableWidget->setItem(i,1,item2);
//            ui->cartTableWidget->setItem(i,2,item3);
//            qDebug() << "3";
//            buyBtnForm* btn = new buyBtnForm;
//            ui->cartTableWidget->setRowHeight(i, 35);
//            btn->resize(172, 35);
//            ui->cartTableWidget->setCellWidget(i, 3, btn);
//            qDebug() << "4";
//            //将要修改或删除的行号通过信号和槽发送到控件中
//            connect(this, &PuyWindow::sendRowBuyAddCart, btn, &buyBtnForm::getRowCartBuyAdd);
//            connect(this, &PuyWindow::sendRowBuySubCart, btn, &buyBtnForm::getRowCartBuySub);
//            emit sendRowBuyAddCart(1, i, buyCountRecord[i]);
//            emit sendRowBuySubCart(1, i, buyCountRecord[i]);
//            disconnect(this, &PuyWindow::sendRowBuyAddCart, btn, &buyBtnForm::getRowCartBuyAdd);
//            disconnect(this, &PuyWindow::sendRowBuySubCart, btn, &buyBtnForm::getRowCartBuySub);
//            connect(btn, &buyBtnForm::sendRowSubCart, this, &PuyWindow::getSubCart);
//            connect(btn, &buyBtnForm::sendRowAddCart, this, &PuyWindow::getAddCart);
//            qDebug() << "5";
//            totelPricesCart += (goodList[buyRowRecord[i]].at(1).toFloat()* buyCountRecord[i]);
//            ui->cartMoneyLabel->setText(QString::number(totelPricesCart));
//        }
//    }

}

void PuyWindow::on_reflushCartBtn_clicked()
{
    qDebug() << "buyRowRecord Size:" << buyRowRecord.size();
    for(int i = 0; i < buyRowRecord.size(); i++)
    {
        qDebug() << "buyCountRecord[buyRowRecord[i]:" << buyCountRecord[i];
        if(buyCountRecord[i] == 0)
        {
            buyRowRecord.removeAt(i);
            buyCountRecord.removeAt(i);
        }
    }


    qDebug() << "buyRowRecord:" << buyRowRecord;

    qDebug() << "buyCountRecord:" << buyCountRecord;

    on_shoppingCartInterfaBtn_clicked();

}

void PuyWindow::on_cartBuyBtn_clicked()
{

    QDateTime curDateTime=QDateTime::currentDateTime();
    qDebug() << "当前日期：" << curDateTime.toString("yyyy/MM/dd");
    qDebug() << "当前时间：" << QTime::currentTime().toString("hh:mm:ss");

    //将用户的购物车中的信息加进购买paidMap中
    QMap<QString, QList<QStringList>>::const_iterator i;

    for(i=cartMap.constBegin(); i!=cartMap.constEnd(); ++i)
    {
        if(i.key() == u_Name)
        {
            QStringList t_paidSL;
            QList<QStringList> tmpL = i.value();
            for (int i = 0; i < tmpL.size(); i++)
            {
                QStringList tmpSL = tmpL[i];
                QString tmp = curDateTime.toString("yyyy/MM/dd") + "-" + QTime::currentTime().toString("hh:mm:ss") + "-" + tmpSL[0] + "-" + tmpSL[1] + "-" + tmpSL[2] + "-" + tmpSL[3];
                t_paidSL << tmp;
            }
            paidList.push_back(t_paidSL);
            paidMap.insert(u_Name, paidList);
        }
    }
    qDebug() << "paidMap:" << paidMap;
    updataOrder();
    //将信息更新到订单列表

    for ( i=cartMap.constBegin(); i!=cartMap.constEnd(); ++i)
    {
        if(i.key() == u_Name)
        {
            QList<QStringList> tmp;
            tmp = i.value();
            tmp.clear();
            cartMap[i.key()] = tmp;
            break;
        }
    }
//    qDebug() << "购买cartMap：" << cartMap;

    totelPrices = 0;
    totelPricesCart = 0;
    ui->cartMoneyLabel->setText(QString::number(totelPricesCart));
    updataMap();
    on_shoppingCartInterfaBtn_clicked();

}


/*
 *
 *  订单界面模块：
 *          功能：查看已购买商品信息
 *          函数：
 *              void PuyWindow::on_buyRecordInterfaBtn_clicked() 点击订单界面
 *              void PuyWindow::updataOrder() 更新订单信息
 *
*/
void PuyWindow::on_buyRecordInterfaBtn_clicked()
{
    totelPrices = 0;
    totelPricesCart = 0;
    //隐藏组件
//    ui->goodComboBox->hide();
//    ui->searchEdit->hide();
//    ui->searchBtn->hide();
    ui->goodsTableWidget->hide();
//    ui->cancelBtn->hide();
    ui->reflushBtn->hide();
    ui->orderTableWidget->hide();
    ui->label_6->hide();
    ui->moneyLabel->hide();
    ui->label_7->hide();
    ui->cartTableWidget->hide();
    ui->cartBuyBtn->hide();
    ui->cartMoneyLabel->hide();
    ui->reflushCartBtn->hide();
    ui->widget_5->hide();
    ui->widget_3->hide();
    ui->widget_4->hide();
    ui->textBrowser->hide();
    //显示组件
    ui->orderTableWidget->show();

    QFile file_order("./order.json");
    if(!file_order.open(QIODevice::ReadWrite))
    {
        qDebug() << "打开文件失败";
    }
    QByteArray arry = file_order.readAll();
    QJsonDocument doc = QJsonDocument::fromJson(arry);
    QJsonObject obj = doc.object();
    QJsonArray obj_arry = obj.value("users").toArray();
    for (int i = 0; i < obj_arry.size(); i++)
    {
        QJsonObject userObj = obj_arry[i].toObject();
        if(userObj.value("name") == u_Name)
        {
            QJsonArray userArr = userObj.value("cart").toArray();
            if(userArr.size() == 0)
            {
                ui->orderTableWidget->setRowCount(userArr.size());
                ui->widget_5->show();
            }
            else
            {
                //设置表格的行数等于用户信息的数量
                ui->orderTableWidget->setRowCount(userArr.size());
                for (int i = 0; i < userArr.size(); i++)
                {
                    QString str = userArr[i].toString();
                    paidStringList = str.split("-");
                    float t_p = paidStringList[3].toFloat() * paidStringList[5].toInt();
                    qDebug() << "日期" << paidStringList[0];
                    qDebug() << "时间" << paidStringList[1];
                    qDebug() << "商品名" << paidStringList[2];
                    qDebug() << "售价" << paidStringList[3];
                    qDebug() << "种类" << paidStringList[4];
                    qDebug() << "数量" << paidStringList[5];
                    QTableWidgetItem* item1 = new QTableWidgetItem(paidStringList[0]);
                    QTableWidgetItem* item2 = new QTableWidgetItem(paidStringList[1]);
                    QTableWidgetItem* item3 = new QTableWidgetItem(paidStringList[2]);
                    QTableWidgetItem* item4 = new QTableWidgetItem(paidStringList[3]);
                    QTableWidgetItem* item5 = new QTableWidgetItem(paidStringList[4]);
                    QTableWidgetItem* item6 = new QTableWidgetItem(paidStringList[5]);
                    QTableWidgetItem* item7 = new QTableWidgetItem(QString::number(t_p));
                    item1->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                    item2->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                    item3->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                    item4->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                    item5->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                    item6->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                    item7->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
                    ui->orderTableWidget->setItem(i,0,item1);
                    ui->orderTableWidget->setItem(i,1,item2);
                    ui->orderTableWidget->setItem(i,2,item3);
                    ui->orderTableWidget->setItem(i,3,item4);
                    ui->orderTableWidget->setItem(i,4,item5);
                    ui->orderTableWidget->setItem(i,5,item6);
                    ui->orderTableWidget->setItem(i,6,item7);

                }

            }
        }
    }
}

void PuyWindow::updataOrder()
{
    int index = 0;
    QList<QStringList> tmp = paidMap.value(u_Name);
    QJsonArray tmpArry;
    qDebug() << "tmpOrder.size:" << tmp.size();
    qDebug() << "tmpOrder:" << tmp;
    QStringList tmpSL = tmp[0];
    for (int i= 0; i < tmpSL.size(); i++)
    {
        tmpArry.insert(i, tmpSL[i]);
        index = i;
    }
//    QJsonObject tmpObj;
//    tmpObj.insert("cart", tmpArry);
//    tmpObj.insert("name", u_Name);

    QFile file("./order.json");

    if(!file.open(QIODevice::ReadWrite))
    {
       qDebug() << "文件打开失败";
    }
    QByteArray array =  file.readAll();
    QJsonDocument doc = QJsonDocument::fromJson(array);
    QJsonObject usersObj = doc.object();
    QJsonArray resultArray = usersObj.value("users").toArray();
    QJsonArray newArry;
    QJsonObject m_userObj;
    for (int i = 0; i < resultArray.size(); i++)
    {
        m_userObj = resultArray[i].toObject();
        if(m_userObj.value("name") == u_Name)
        {
            QJsonArray tmpA = m_userObj.value("cart").toArray();
            for (int i= 0; i <tmpA.size(); i++)
            {
                tmpArry.insert(index, tmpA[i]);
                index++;
            }
            QJsonObject tmpObj;
            tmpObj.insert("cart", tmpArry);
            tmpObj.insert("name", u_Name);
            m_userObj = tmpObj;
            newArry.push_back(m_userObj);

        }
        else
        {
            newArry.push_back(m_userObj);
        }
    }

    QJsonObject newOrder;
    newOrder.insert("users",newArry);
    QJsonDocument n_doc(newOrder);
    file.resize(0);
    file.write(n_doc.toJson());
    file.close();
}


/*
 *  联系我们界面：
 *          功能：说明性文档显示
 *          函数：
 *              void PuyWindow::on_contactBtn_clicked() 点击联系我们界面
*/
void PuyWindow::on_contactBtn_clicked()
{
    totelPrices = 0;
    totelPricesCart = 0;
    //隐藏组件
//    ui->goodComboBox->hide();
//    ui->searchEdit->hide();
//    ui->searchBtn->hide();
    ui->goodsTableWidget->hide();
//    ui->cancelBtn->hide();
    ui->reflushBtn->hide();
    ui->orderTableWidget->hide();
    ui->label_6->hide();
    ui->moneyLabel->hide();
    ui->label_7->hide();
    ui->cartTableWidget->hide();
    ui->cartBuyBtn->hide();
    ui->cartMoneyLabel->hide();
    ui->reflushCartBtn->hide();
    ui->widget_5->hide();
    ui->widget_3->hide();
    ui->widget_4->hide();
    ui->orderTableWidget->hide();
    //显示的界面
    ui->textBrowser->show();
}
